package com.ffyc.cms.controller;

import com.ffyc.cms.common.CommonResult;
import com.ffyc.cms.model.Admin;
import com.ffyc.cms.model.Menu;
import com.ffyc.cms.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/loginadmin")
public class LoginController {
    @Autowired
    LoginService loginService;
    /*
    22
     */
    @PostMapping(path = "/login")
    public CommonResult login(Admin admin, HttpSession session){
        CommonResult commonResult = null;
        try {
            Admin admin1 = loginService.login(admin);
            commonResult = new CommonResult<Admin>(200,"登录成功" , admin1);
            session.setAttribute("admin",admin1);
        }catch (Exception e){
            e.printStackTrace();
            commonResult = new CommonResult<Admin>(500,"登录失败" , null);
        }
        return commonResult;
    }

    @GetMapping(path = "/loginOut")
    public void loginOut(HttpSession session){
        session.removeAttribute("admin");
    }

    @GetMapping(path = "/findMenu")
    public CommonResult login(Admin admin){
        CommonResult commonResult = null;
        try {
            List<Menu> menus = loginService.findMenu(admin);
            commonResult = new CommonResult<List<Menu>>(200,"查找菜单成功" , menus);
        }catch (Exception e){
            commonResult = new CommonResult<>(500,"查找菜单失败" , null);
        }
        return commonResult;
    }
}
